from apps.common.base.serializers import *
from apps.system.role.models import *
from apps.system.router.serializers import RouterAllListSerializer


class RoleBaseSerializer(BaseSerializer):
    class Meta:
        model = Role
        fields = "__all__"


class RoleModelSerializer(RoleBaseSerializer):


    # 嵌套菜单数据
    routerList = serializers.SerializerMethodField()
    def get_routerList(self, obj):
        if obj.routers:
            return RouterAllListSerializer(
                obj.routers.all().filter(is_delete=0).exclude(is_root=True).order_by('-sort'), many=True).data
        else:
            return []

    class Meta:
        model = Role
        fields = "__all__"
